package com.example.demo.web;

import com.example.demo.address.bean.AddressBean;
import com.example.demo.address.service.AddressService;
import com.example.demo.banner.bean.BannerBean;
import com.example.demo.banner.service.BannerService;
import com.example.demo.mylove.bean.MyLoveBean;
import com.example.demo.mylove.service.MyLoveService;
import com.example.demo.news.service.NewsService;
import com.example.demo.news.service.NewsTypeService;
import com.example.demo.shops.bean.CarBean;
import com.example.demo.shops.service.ShopService;
import com.example.demo.sicktype.bean.SickTypeBean;
import com.example.demo.sicktype.service.SickTypeService;
import com.example.demo.user.bean.UserBean;
import com.example.demo.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("web")
public class indexController {

    @Autowired
    BannerService bannerService;

    @Autowired
    NewsService newsService;

    @Autowired
    NewsTypeService newsTypeService;

    @Autowired
    SickTypeService sickTypeService;

    @Autowired
    ShopService shopService;


    @Autowired
    UserService userService;

    @Autowired
    AddressService addressService;


    @Autowired
    MyLoveService myLoveService;

    @RequestMapping("index")
    public String index(Model model){
        List<BannerBean> list =bannerService.getAll();
        if (list.isEmpty()){
            list.add(new BannerBean());
        }
        model.addAttribute("banner",list);
//        ArrayList arrayList = new ArrayList();
//        arrayList.get(arrayList.size())
        model.addAttribute("news",newsService.getNewsByType(new PageRequest(0,6),newsTypeService.getById(1)));
        return "web/index";
    }


    @RequestMapping("category")
    public String category(Model model){
        model.addAttribute("list",sickTypeService.getAll());
        return "web/category";
    }

    @RequestMapping("shops/{typeid}")
    public String shops(@PathVariable int typeid, Model model){
        SickTypeBean bean = sickTypeService.getById(typeid);
        model.addAttribute("name",bean.getTitle());
        model.addAttribute("list",shopService.getShopsBySitckType(bean));
        return "web/shops";
    }

    @RequestMapping("page/{shopis}")
    public String getshop(@PathVariable int shopis, Model model){
        model.addAttribute("entity",shopService.getById(shopis));
        return "web/page";
    }

    @RequestMapping("car")
    public String car(Model model,HttpSession session){
        HashMap<Integer,Integer> hashMap = (HashMap<Integer, Integer>) session.getAttribute("SHOPLIST");
        List<CarBean> list = shopService.getcarlist(hashMap);
       double allprice = 0;
       if (list!=null&&!list.isEmpty()) {
           allprice = list.parallelStream().mapToDouble(b ->
                   Double.valueOf(b.getPrice()) * b.getNumber()
           ).sum();
       }
        model.addAttribute("carlist",list);
        model.addAttribute("allprice",allprice);
        return "web/car";
    }

    @RequestMapping("me")
    public String me(Model model,HttpSession session){
        UserBean userBean = (UserBean) session.getAttribute("USERID");
        if (userBean==null){
            return "redirect:/web/index";
        }


        model.addAttribute("address",addressService.getAddListByUser(userBean));
        model.addAttribute("usermodel",userBean);
        return "web/me";
    }

    @RequestMapping(value = "addaddress",method = RequestMethod.GET)
    public String addaddress(Model model){
//        UserBean userBean = (UserBean) session.getAttribute("USERID");
//        if (userBean==null){
//            return "redirect:/web/index";
//        }
        model.addAttribute("address",new AddressBean());
        return "web/addaddress";
    }


    @RequestMapping(value = "addaddress",method = RequestMethod.POST)
    public String addaddress(AddressBean addressBean,Model model,HttpSession session){
        UserBean userBean = (UserBean) session.getAttribute("USERID");
        if (userBean==null){
            return "redirect:/web/index";
        }
        addressService.save(addressBean,userBean);
        return "redirect:/web/address";
    }


    @RequestMapping(value = "editaddress/{id}",method = RequestMethod.GET)
    public String editAddress(@PathVariable String id,Model model,HttpSession session){
        UserBean userBean = (UserBean) session.getAttribute("USERID");
        if (userBean==null){
            return "redirect:/web/index";
        }
        AddressBean addressBean = addressService.getById(Integer.parseInt(id));
        if (addressBean==null||addressBean.getUserBean() != userBean)
        {
            return "redirect:/web/address";
        }
        model.addAttribute(addressBean);
        return "web/addaddress";
    }


    @RequestMapping(value = "deladdress/{id}",method = RequestMethod.GET)
    public String delAddress(@PathVariable String  id,HttpSession session) {
        UserBean userBean = (UserBean) session.getAttribute("USERID");
        if (userBean==null){
            return "redirect:/web/index";
        }
        AddressBean addressBean = addressService.getById(Integer.parseInt(id));
        if (addressBean==null||addressBean.getUserBean() != userBean)
        {
            return "redirect:/web/address";
        }
        addressService.delEntity(Integer.parseInt(id));
        return "redirect:/web/address";
    }




    @RequestMapping("address")
    public String address(Model model,HttpSession session){
        UserBean userBean = (UserBean) session.getAttribute("USERID");
        if (userBean==null){
            return "redirect:/web/index";
        }
        model.addAttribute("address",addressService.getAddListByUser(userBean));
        return "web/address";
    }

    @RequestMapping("addshop/{id}")
    @ResponseBody
    public String addShop(@PathVariable int id, HttpSession session){
        UserBean userBean = (UserBean) session.getAttribute("USERID");
        if (userBean==null){
            return "redirect:/web/index";
        }
        HashMap<Integer,Integer> shops = (HashMap<Integer, Integer>) session.getAttribute("SHOPLIST");
        if (shops==null)
        {
            shops = new HashMap<Integer, Integer>();
            shops.put(id,1);
        }else{
            if (shops.get(id)!=null) {
                int Temp = shops.get(id);

                Temp++;
                shops.put(id, Temp);
            }else{
                shops.put(id,1);
            }
        }
        session.setAttribute("SHOPLIST",shops);

        return "添加成功";
    }


    @RequestMapping("addlove/{id}")
    @ResponseBody
    public String addLove(@PathVariable String id,HttpSession session){
        UserBean userBean = (UserBean) session.getAttribute("USERID");
        if (userBean==null){
            return "redirect:/web/index";
        }
        myLoveService.save(Long.parseLong(id),userBean);
        return "添加成功";
    }


    @RequestMapping("mylovelist")
    public String LoveList(HttpSession session,Model model){
        UserBean userBean = (UserBean) session.getAttribute("USERID");
        if (userBean==null){
            return "redirect:/web/index";
        }
        model.addAttribute("list",myLoveService.getAllByUser(userBean));
        return "/web/mylove";
    }

    @RequestMapping("dellove/{id}")
    public String delLove(@PathVariable String id,HttpSession session){
        UserBean userBean = (UserBean) session.getAttribute("USERID");
        if (userBean==null){
            return "redirect:/web/index";
        }
        myLoveService.dellove(id,userBean);
        return "redirect:/web/mylovelist";
    }


    @RequestMapping(value = "createOrder",method = RequestMethod.POST)
    public String createOrder(String[] checkbox){
        System.out.println(checkbox[0]);

        return "redirect:/web/index";
    }

    @RequestMapping("testlogin")
    public String login(HttpSession session){

        UserBean userBean = userService.getById(1);
        if (userBean==null){
            userBean = new UserBean();
            userBean.setNickname("zgscwjm");
            userBean.setHeadimg("http://img.qqzhi.com/upload/img_3_891050437D378578828_23.jpg");
           userBean = userService.save(userBean);
        }
        session.setAttribute("USERID",userBean);
        return "redirect:/web/index";
    }




}
